/*
 * Copyright (C) 2011 Macambira Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.googlecode.control;

import com.googlecode.entity.Departamento;
import com.googlecode.exception.DepartamentoNaoEncontradoException;
import com.googlecode.persistence.DepartamentoDAO;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Classe responsavel por realizar o CRUD de departamento
 *
 * @version 0.2 28 Set 2011
 * @author Ihago Alencar | ihago.alencar@dce.ufpb.br
 */
public class ControlDepartamento {

    private List<Departamento> departamentos;
    private DepartamentoDAO dao;

    public ControlDepartamento() {
        this.departamentos = new LinkedList<Departamento>();
        this.dao = new DepartamentoDAO();
    }

    public void addDepartamento(Departamento departamento) throws SQLException {
        try {
            //        this.departamentos.add(departamento);
            dao.CadastrarDepartamento(departamento);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
 
    public Departamento getDepartamentoNome(String nome) throws SQLException {
        try {
            //        for(Departamento d: departamentos) {
            //            if (d.getNome().equals(nome)) {
            //                return d;
            //            }
            //        }
            //        throw new DepartamentoNaoEncontradoException("Departamento nao encontrado");
            return dao.ConsultaDepartamentoNome(nome);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
        return null;    
    }

    public Departamento getDepartamentoId(String id) throws SQLException {
        try {
            //        for(Departamento d: departamentos) {
            //            if (d.getId().equals(id)) {
            //                return d;
            //            }
            //        }
            //      throw new DepartamentoNaoEncontradoException("Departamento nao encontrado");        
            return dao.ConsultaDepartamentoID(id);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
        return null;
    }

    public void removeDepartamentoNome(String nome) throws SQLException {
        try {
            //        for(Departamento d: departamentos) {
            //            if (d.getNome().equals(nome)) {
            //                this.departamentos.remove(d);
            //                break;
            //            }
            //        }
                    dao.RemoverDepartamentoNome(nome);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }

    public void removeDepartamentoId(String id) throws SQLException {
        try {
            //        for(Departamento d: departamentos) {
            //            if (d.getId().equals(id)) {
            //                this.departamentos.remove(d);
            //                break;
            //            }
            //        }
            dao.RemoverDepartamentoID(id);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }

    public int getSizeDepartamento() {
        return this.departamentos.size();
    }
    
    public List<Departamento> getListDepartamento(){
        return this.departamentos;
    }

}
